package com.css.fxfzysx.modules.vabStructure.repository.impl;

import com.css.fxfzysx.modules.cztDataMange.entity.HbpInfoEntity;
import com.css.fxfzysx.modules.vabStructure.repository.FshBuildingsRepository;
import com.css.fxfzysx.modules.vabStructure.vo.HbpInfoVo;
import com.css.fxfzysx.util.CreateGson;
import com.css.fxfzysx.util.PlatformObjectUtils;
import com.css.fxfzysx.web.PlatformSessionContext;
import com.css.fxfzysx.zcpt.sys.entity.SUser;
import com.css.fxfzysx.zcpt.sys.service.SUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Repository
public class FshBuildingsRepositoryImpl implements FshBuildingsRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private SUserService userService;

    @Override
    public HashMap<String, Object> queryFshBuildings(HbpInfoEntity hbpInfoEntity, int page, int size) {
        StringBuilder sql = new StringBuilder("select * from czt_high_building_param_info where del_flag = '0'");
        if (hbpInfoEntity.getRole().equals("1")) {
            sql.append(" and create_user='" + PlatformSessionContext.getUserID() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(hbpInfoEntity.getProvince())) {
            sql.append(" and province ='" + hbpInfoEntity.getProvince() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(hbpInfoEntity.getCity())) {
            sql.append(" and city ='" + hbpInfoEntity.getCity() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(hbpInfoEntity.getCounty())) {
            sql.append(" and county ='" + hbpInfoEntity.getCounty() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(hbpInfoEntity.getYears())) {
            sql.append(" and years ='" + hbpInfoEntity.getYears() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(hbpInfoEntity.getCreateStartTime())) {
            sql.append(" and create_time >= '" + hbpInfoEntity.getCreateStartTime() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(hbpInfoEntity.getCreateEndTime())) {
            String endTime = hbpInfoEntity.getCreateEndTime() + " 23:59:59";
            sql.append(" and create_time <='" + endTime + "'");
        }
        if (!PlatformObjectUtils.isEmpty(hbpInfoEntity.getStructureType())) {
            sql.append(" and structure_type ='" + hbpInfoEntity.getStructureType() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(hbpInfoEntity.getStructureType1())) {
            sql.append(" and structure_type1 ='" + hbpInfoEntity.getStructureType1() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(hbpInfoEntity.getFortification())) {
            sql.append(" and fortification ='" + hbpInfoEntity.getFortification() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(hbpInfoEntity.getPurpose())) {
            sql.append(" and purpose ='" + hbpInfoEntity.getPurpose() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(hbpInfoEntity.getPurpose1())) {
            sql.append(" and purpose1 ='" + hbpInfoEntity.getPurpose1() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(hbpInfoEntity.getUserName())) {
            sql.append(" and purpose1 like'%" + hbpInfoEntity.getUserName() + "%'");
        }
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(HbpInfoEntity.class)).size();
        StringBuilder orderSql = new StringBuilder("order by create_time desc");
        StringBuilder limitSql = new StringBuilder(" limit " + size + " offset " + (page - 1) * size);
        sql.append(orderSql).append(limitSql);
        List<HbpInfoEntity> data = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(HbpInfoEntity.class));
        for (HbpInfoEntity datum : data) {
            if (PlatformObjectUtils.isNotEmpty(datum.getCreateUser())) {
                String user = userService.getSUser(datum.getCreateUser());
                SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
                datum.setUserName(sUser.getUserName());
            }
            if (PlatformObjectUtils.isNotEmpty(datum.getProvince()) && PlatformObjectUtils.isNotEmpty(datum.getCity()) && PlatformObjectUtils.isNotEmpty(datum.getCounty())) {
                StringBuilder region = new StringBuilder(datum.getProvince()).append("-").append(datum.getCity()).append("-").append(datum.getCounty());
                datum.setRegion(region.toString());
            }
            if (PlatformObjectUtils.isNotEmpty(datum.getStructureType())) {
                StringBuilder st = new StringBuilder(datum.getStructureType());
                if(PlatformObjectUtils.isNotEmpty(datum.getStructureType1())){
                    st.append("-"+datum.getStructureType1());
                }
                datum.setSt(st.toString());
            }
            if (PlatformObjectUtils.isNotEmpty(datum.getPurpose()) && PlatformObjectUtils.isNotEmpty(datum.getPurpose1())) {
                StringBuilder use = new StringBuilder(datum.getPurpose()).append("-").append(datum.getPurpose1());
                datum.setUse(use.toString());
            }
        }
        HashMap<String, Object> resultMap = new HashMap();
        resultMap.put("total", total);
        resultMap.put("rows", data);
        return resultMap;
    }

    @Override
    public void modifyCalculationStatus(HbpInfoVo entity) {
        String sql = "update czt_high_building_param_info set forecast_results6 = '"+entity.getForecastResults6()+"', forecast_results7 = '"+entity.getForecastResults7()+"', forecast_results8 = '"+entity.getForecastResults8()+"', forecast_results9 = '"+entity.getForecastResults9()+"', forecast_results10 = '"+entity.getForecastResults10()+"', emid_angle6 ='"+entity.getEmidAngle6()+"',emid_angle7 = '"+entity.getEmidAngle7()+"', emid_angle8 = '"+entity.getEmidAngle8()+"', emid_angle9 = '"+entity.getEmidAngle9()+"' , emid_angle10 = '"+entity.getEmidAngle10()+"' ,correction_coefficient = '"+entity.getCorrectionCoefficient()+"' ,calculate_or_not = '1'  where chbpi_id= '"+entity.getChbpiId()+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void updateFshBuildings(HbpInfoEntity entity) {
        jdbcTemplate.update("UPDATE  czt_high_building_param_info  SET intensity=?,damage=?,province=?,city=?,county=?,purpose=?,structure_type=?,years=?,fortification=?,cs=?,site=?,build_area=?,building_height=?,floor_height=?,txzdcjwyj6=?,update_user=?,update_time=?,del_flag=?,purpose1=?,structure_type1=?,data_type=?,number=?,extends1=?,extends2=?,extends3=?,extends4=?,extends5=?,extends6=?,extends7=?,extends8=?,extends9=?,extends10=?,cs1=?,calculate_or_not=?,forecast_results6=?,emid_angle6=?,describe=?,create_unit=?,basic_library=?,correction_coefficient=?,emid_angle7=?,emid_angle8=?,emid_angle9=?,emid_angle10=?,txzdcjwyj7=?,txzdcjwyj8=?,txzdcjwyj9=?,txzdcjwyj10=?,forecast_results7=?,forecast_results8=?,forecast_results9=?,forecast_results10=?"
                        +"where chbpi_id=?",
                entity.getIntensity(),entity.getDamage(),entity.getProvince(),entity.getCity(),entity.getCounty(),entity.getPurpose(),entity.getStructureType(),entity.getYears(),entity.getFortification(),entity.getCs(),entity.getSite(),entity.getBuildArea(),entity.getBuildingHeight(),entity.getFloorHeight(),entity.getTxzdcjwyj6(),entity.getUpdateUser(),entity.getUpdateTime(),entity.getDelFlag(),entity.getPurpose1(),entity.getStructureType1(),entity.getDataType(),entity.getNumber(),entity.getExtends1(),entity.getExtends2(),entity.getExtends3(),entity.getExtends4(),entity.getExtends5(),entity.getExtends6(),entity.getExtends7(),entity.getExtends8(),entity.getExtends9(),entity.getExtends10(),entity.getCs1(),entity.getCalculateOrNot(),entity.getForecastResults6(),entity.getEmidAngle6(),entity.getDescribe(),entity.getCreateUnit(),entity.getBasicLibrary(),entity.getCorrectionCoefficient(),entity.getEmidAngle7(),entity.getEmidAngle8(),entity.getEmidAngle9(),entity.getEmidAngle10(),entity.getTxzdcjwyj7(),entity.getTxzdcjwyj8(),entity.getTxzdcjwyj9(),entity.getTxzdcjwyj10(),entity.getForecastResults7(),entity.getForecastResults8(),entity.getForecastResults9(),entity.getForecastResults10(),
                entity.getChbpiId()
        );
    }

    @Override
    public HbpInfoEntity queryFshBuildingsById(String chbpiId) {
        String sql = "select * from czt_high_building_param_info where del_flag='0' and chbpi_id='" + chbpiId + "'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(HbpInfoEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void saveFshBuildings(HbpInfoEntity entity) {
        jdbcTemplate.update("insert into czt_high_building_param_info  (chbpi_id,intensity,damage,province,city,county,purpose,structure_type,years,fortification,cs,site,build_area,building_height,floor_height,txzdcjwyj6,create_user,create_time,update_user,update_time,del_flag,purpose1,structure_type1,data_type,number,extends1,extends2,extends3,extends4,extends5,extends6,extends7,extends8,extends9,extends10,cs1,calculate_or_not,forecast_results6,emid_angle6,user_name,describe,create_unit,basic_library,correction_coefficient,emid_angle7,emid_angle8,emid_angle9,emid_angle10,txzdcjwyj7,txzdcjwyj8,txzdcjwyj9,txzdcjwyj10,forecast_results7,forecast_results8,forecast_results9,forecast_results10 ) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? )",
                entity.getChbpiId(),entity.getIntensity(),entity.getDamage(),entity.getProvince(),entity.getCity(),entity.getCounty(),entity.getPurpose(),entity.getStructureType(),entity.getYears(),entity.getFortification(),entity.getCs(),entity.getSite(),entity.getBuildArea(),entity.getBuildingHeight(),entity.getFloorHeight(),entity.getTxzdcjwyj6(),entity.getCreateUser(),entity.getCreateTime(),entity.getUpdateUser(),entity.getUpdateTime(),entity.getDelFlag(),entity.getPurpose1(),entity.getStructureType1(),entity.getDataType(),entity.getNumber(),entity.getExtends1(),entity.getExtends2(),entity.getExtends3(),entity.getExtends4(),entity.getExtends5(),entity.getExtends6(),entity.getExtends7(),entity.getExtends8(),entity.getExtends9(),entity.getExtends10(),entity.getCs1(),entity.getCalculateOrNot(),entity.getForecastResults6(),entity.getEmidAngle6(),entity.getUserName(),entity.getDescribe(),entity.getCreateUnit(),entity.getBasicLibrary(),entity.getCorrectionCoefficient(),entity.getEmidAngle7(),entity.getEmidAngle8(),entity.getEmidAngle9(),entity.getEmidAngle10(),entity.getTxzdcjwyj7(),entity.getTxzdcjwyj8(),entity.getTxzdcjwyj9(),entity.getTxzdcjwyj10(),entity.getForecastResults7(),entity.getForecastResults8(),entity.getForecastResults9(),entity.getForecastResults10());
    }

    @Override
    public void moveBasicLibraryById(String dataId,String operate) {
        String sql = "update czt_high_building_param_info set basic_library=? where chbpi_id = ?";
        jdbcTemplate.update(sql,operate,dataId);
    }

    @Override
    public void updateCountStatusById(String chamId) {
        String sql = "update czt_high_building_param_info set calculate_or_not = '0' where correction_coefficient like '%"+chamId+"%'";
        jdbcTemplate.update(sql);
    }
}
